<div id="user_rapoarte_facturi_scadente"></div>

<script type="text/javascript">	
		
Ext.onReady(function(){	
	
	var ds_facturi_scadente;
	var cm_facturi_scadente;
	var grid_user_rapoarte_facturi_scadente;	

	function getExcel() {
        if (!Ext.fly('tbExcel')) {
            return {
                xtype: 'tbbutton',
                id: 'tbExcel',
				text:'Excel',
				tooltip:'Exporta in excel',
				iconCls:'b_excel_cls',
                handler: function() {
                    //create dummy form
                    if (!Ext.fly('frmDummy')) {
                        var frm = document.createElement('form');
                        frm.id = 'frmDummy';
                        frm.name = id;
                        frm.className = 'x-hidden';
                        document.body.appendChild(frm);
                    }

                    //get attachment
						Ext.Ajax.request({
						   url: 'user/functionalitati/excel/' + Math.random(),
						   disableCaching	:false,
						   isUpload			:true,
						   params:{
								saferequest	:true,
								table		:'facturi_scadente',
								filename	: 'raport',
								cyDep		:true,	
								active		:false,
								start 		:grid_user_rapoarte_facturi_scadente.store.lastOptions.params.start,
								limit		:grid_user_rapoarte_facturi_scadente.store.lastOptions.params.limit,
								sort		:grid_user_rapoarte_facturi_scadente.store.lastOptions.params.sort,
								dir			:grid_user_rapoarte_facturi_scadente.store.lastOptions.params.dir,
								fields		:grid_user_rapoarte_facturi_scadente.store.lastOptions.params.fields,
								query		:grid_user_rapoarte_facturi_scadente.store.lastOptions.params.query								   	
						   },
                        form: Ext.fly('frmDummy')
                    });
                }
            };

        } else {
            return Ext.getCmp('tbExcel');
        }
    } 	

/**
 * grid
 * ************************************************************************
 */	
		function rdr_dtp(val){
			if (val < 0){
				return "<span style='color:red;'>" + val + " &laquo;</span>";
			} else if(val == 0){
				return "<span style='color:blue;'>" + val + " &lsaquo;&rsaquo;</span>";
			} else if(val > 0){
				return "<span style='color:green;'>&raquo;" + val + "</span>";
			}
		}	
					
	    ds_facturi_scadente = new Ext.data.Store({
			id			:'table_ds_facturi_scadente',
	        remoteSort	:true,
			baseParams	:{saferequest:true,table:'facturi_scadente',cyDep:true},			
	        proxy: new Ext.data.HttpProxy({
	            url: 'common/getNotPayedBillsTable/' + Math.random()		
	        }),
	        reader: new Ext.data.JsonReader({
	            root: 'DS_FACTURI_SCADENTE',
	            totalProperty: 'totalCount',
	            id: 'ID',
	            fields: [
	                'numar', 'client', 'data_emiterii', 'data_scadentei', 'overdue', 'moneda', 'facturat', 'incasat', 'sold'
	            ]
	        })
	    });	

		ds_facturi_scadente.setDefaultSort('numar', 'desc');
	    ds_facturi_scadente.load({params:{start:0, limit:22}});
					
	    cm_facturi_scadente = new Ext.grid.ColumnModel([
				{header: "Numar",				dataIndex: 'numar'},
				{header: "Client",				dataIndex: 'client'},
				{header: "Data emiterii",		dataIndex: 'data_emiterii'},
				{header: "Data scadentei",		dataIndex: 'data_scadentei'},
				{header: "DTP",					dataIndex: 'overdue',	renderer:rdr_dtp},
				{header: "Moneda",				dataIndex: 'moneda'},
				{header: "Facturat",			dataIndex: 'facturat'},
				{header: "Incasat",				dataIndex: 'incasat'},	
				{header: "Sold",				dataIndex: 'sold'}		
		]);
		
    	cm_facturi_scadente.defaultSortable = true;	
		
	    grid_user_rapoarte_facturi_scadente = new Ext.grid.GridPanel({
			id:'gridfacturiscadente-' + Math.random(),
	        renderTo:'user_rapoarte_facturi_scadente',
			height:569,
			anchor:'100%',	
			frame:false,					
	        title:'<?=$title?>',
	        store: ds_facturi_scadente,
	        cm: cm_facturi_scadente,
	        loadMask: true,
	        viewConfig: {
	            forceFit:true							
	        },
	        tbar:[getExcel()],		
	        bbar: new Ext.PagingToolbar({
				beforePageText:'Pagina&nbsp;',
				afterPageText:'din {0}',
				firstText:'Prima pagina',
				lastText:'Ultima pagina',
				nextText:'Pagina urmatoare',
				prevText:'Pagina precedenta',   
	            pageSize: 22,
	            store: ds_facturi_scadente,
	            displayInfo: true,
	            displayMsg: 'Facturi scadente {0} - {1} din {2}',
	            emptyMsg: "No records",
				plugins: new Ext.ux.Andrie.pPageSize({
					  beforeText:'',		
				      afterText: '&nbsp;inreg/pagina',
				      comboCfg: {width: 50}
				      })
			}),plugins: new Ext.ux.grid.Search({
						searchText:'Criterii',
						searchTipText:'Introduceti textul dorit...',
						iconCls:'b_search_cls',
						minChars:2,
						autoFocus:true,
						text:'Cauta dupa camp',
						mode:'remote',
						position:'top',
						disableIndexes:['overdue'],
						checkIndexes:['client']
			})
		});		

	//end on document ready
	});
</script>
